<template>
  <div class="mod-config">
    <el-form inline @keyup.enter.native="searchChange()">
      <el-form-item label="粉丝来源" prop="fansSource">
        <el-select v-model="dataForm.fansSource">
          <el-option :value="1" label="主动关注"></el-option>
          <el-option :value="2" label="扫描推广二维码关注"></el-option>
          <el-option :value="3" label="扫描内部员工二维码关注"></el-option>
          <el-option :value="4" label="扫描粉丝二维码关注"></el-option>
          <el-option :value="5" label="扫描外部推广员二维码关注"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="推广员名称" prop="promoterName">
        <el-input v-model="dataForm.promoterName" placeholder="推广员名称"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="telPhone">
        <el-input v-model="dataForm.telPhone" placeholder="联系方式"></el-input>
      </el-form-item>
      <el-form-item label="所属公众号" prop="accountId">
        <el-select v-model="dataForm.accountId" placeholder="请选择公众号">
          <el-option v-for="item in wxAccountList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="粉丝状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="2">所有粉丝 </el-radio>
          <el-radio :label="1">48小时内活跃粉丝</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="分组标签" prop="labelId">
        <el-select v-model="dataForm.labelId" placeholder="请选择分组标签">
          <el-option v-for="item in wxlabelList" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="关注日期">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.joinTimeStart" value-format="yyyy-MM-dd"
            style="width: 100%;"></el-date-picker>
        </el-col>
        <el-col class="line" :span="2" style="text-align: center;">至</el-col>
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.joinTimeEnd" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="粉丝名称" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="粉丝名称"></el-input>
      </el-form-item>
      <el-form-item label="会员名称" prop="personName">
        <el-input v-model="dataForm.personName" placeholder="会员名称"></el-input>
      </el-form-item>
      <el-form-item label="剩余积分">
        <el-col :span="11">
          <el-input v-model="dataForm.integralMin" placeholder=""></el-input>
        </el-col>
        <el-col class="line" :span="2" style="text-align: center;">至</el-col>
        <el-col :span="11">
          <el-input v-model="dataForm.integralMax" placeholder=""></el-input>
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
        <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
      </el-form-item>

    </el-form>
    <el-row>
      <el-col :span="24">
        <el-button v-if="permissions.mp_wxaccountfans_add" icon="el-icon-plus" type="primary" size="mini" @click="addOrUpdateHandle()">新增</el-button>
      </el-col>
    </el-row>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading">
        <el-table-column prop="nickname" label="微信昵称"></el-table-column>
        <el-table-column prop="personName" label="粉丝名称"></el-table-column>
        <!-- <el-table-column prop="subscribeStatus" label="订阅状态"></el-table-column> -->
        <!-- <el-table-column prop="subscribeTime" label="订阅时间"></el-table-column> -->
        <el-table-column prop="gender" label="性别" :formatter="genderFormatter"></el-table-column>
        <!-- <el-table-column prop="headimgUrl" label="头像地址"></el-table-column> -->
        <el-table-column prop="fansSource" label="粉丝来源"></el-table-column>
        <el-table-column prop="promoterId" label="推广员"></el-table-column>
        <el-table-column prop="phone" label="联系方式"></el-table-column>

        <!-- <el-table-column prop="integral" label="微信积分"></el-table-column> -->
        <el-table-column prop="wxAccountName" label="微信公众号"></el-table-column>
        <!-- <el-table-column prop="wxAccountAppid" label="appid"></el-table-column> -->
        <!-- (1-48小时内活跃粉丝,0-非48小时内活跃粉丝) -->
        <el-table-column prop="status" label="粉丝状态"></el-table-column>
        <!-- <el-table-column prop="labelId" label="标签id"></el-table-column> -->
        <el-table-column prop="joinTime" label="关注时间"></el-table-column>
        <!-- <el-table-column prop="personId" label="人员id/会员id"></el-table-column> -->
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="permissions.mp_wxaccountfans_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="permissions.mp_wxaccountfans_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'
  import {
    fetchWxAccountFansList,
    delWxAccountFansObj
  } from '@/api/mp/wxaccountfans'
  import {
    fetchAccountList
  } from '@/api/mp/wxaccount'
  import {
    getWxLabelInfoList
  } from '@/api/mp/wxlabel'
  import TableForm from './wxaccountfans-form'

  export default {
    data() {
      return {
        wxAccountList: [],
        dataForm: {
          status: 2,
          key: ''
        },
        wxlabelList: [],
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created() {
      this.getDataList(),
        this.getAccountList(),
        this.getLabelList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchWxAccountFansList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          console.log(response.data.data.records)
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delWxAccountFansObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        this.$refs.searchForm.resetFields();
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      // 获取微信公众号列表
      getAccountList() {
        fetchAccountList().then(response => {
          this.wxAccountList = response.data.data
        })
      },
      getLabelList() {
        getWxLabelInfoList().then(response => {
          this.wxlabelList = response.data.data
        })
      },
      genderFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 0:
            return '男'
            break
          case 1:
            return '女'
            break
        }
      },
    }
  }
</script>
